package de.joergviola.mobj.shared;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.core.client.GWT;

public class Schema {
	@SuppressWarnings("rawtypes")
	private HashMap<String, Entity> entities = new HashMap<String, Entity>();
	private ArrayList<Class<? extends Persistable>> types = new ArrayList<Class<? extends Persistable>>();
	private String shortName;
	private String version = "1.0";
	private String displayName;
	private int maxSizeBytes = 100000;
	private String remoteURI = "/srv/remoteTx";

	public Schema(String name) {
		shortName = name;
		displayName = name;
	}

	public <T extends Serializable> Entity<T> getEntity(Class<T> clazz) {
		return getEntity(clazz.getName());
	}

	@SuppressWarnings("unchecked")
	public <T extends Serializable> Entity<T> getEntity(T t) {
		return (Entity<T>) getEntity(t.getClass());
	}

	@SuppressWarnings("unchecked")
	public <T extends Serializable> Entity<T> getEntity(String className) {
		return entities.get(className);
	}

	public Iterable<Class<? extends Persistable>> getTypes() {
		return types;
	}

	protected boolean isClient() {
		return GWT.isClient();
	}

	public <T extends Persistable> void register(
			Class<? extends Persistable> type, Entity<T> entity) {
		if (isClient()) {
			entities.put(type.getName(), entity);
		}
		types.add(type);
	}

	public String getShortName() {
		return shortName;
	}

	public void setShortName(String shortName) {
		this.shortName = shortName;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public int getMaxSizeBytes() {
		return maxSizeBytes;
	}

	public void setMaxSizeBytes(int maxSizeBytes) {
		this.maxSizeBytes = maxSizeBytes;
	}

	public String getRemoteURI() {
		return remoteURI;
	}

	public void setRemoteURI(String remoteURI) {
		this.remoteURI = remoteURI;
	}
}
